#!/usr/bin/ruby
# 
# To change this template, choose Tools | Templates
# and open the template in the editor.
 class Loan
   def initialize(loanamount, payment, rate)
     @loanamount = loanamount.to_i
     @payment =payment.to_i
     @rate = rate.to_f
     @interest = 0
     @totalinterest = 0.0
     @totalpayments = 0
     @currentpayment = 0
   end
   #Public methods to access the different variables.
   #
   #laonAmount
   def loanamount
     @loanamount
   end
   def loanamount=(newamount)
     @loanamount = newamount
   end
   
   #Payment
   def payment
      @payment
   end
   def payment=(payment)
    @payment=payment
   end

   #rate
   def rate
     @rate
   end
   def rate=(rate)
     @rate=rate
   end
   
   def calculatepayment
     @interest = @loanamount * @rate
     @totalinterest += @interest
     @currentpayment = (@payment - @interest)
     @loanamount = @loanamount - @currentpayment
     @totalpayments += @payment
     @interest
   end
   def last_payment
     @interest = @loanamount * @rate
     @totalinterest += @interest
     @currentpayment = @interest + @loanamount
     @totalpayments += @currentpayment
     @loanamount = 0
   end
   #print the header of the output
   def printheader
     puts " # | Balance | Interest | NetPayment | TotalPayments | TotalInterest"
   end
   #print a single line that contains all the info
   def printloan(id)
     print " "
     print id 
     print " |    "
     print @loanamount.truncate
     print "  |   "
     print @interest.truncate
     print "    |      "
     print @currentpayment.truncate
     print "     |      "
     print @totalpayments.truncate
     print "       |      "
     print @totalinterest.truncate
     print "\n"
   end
   
   #format a number or string for printing
   def preparevalue(value, places)
     
   end
   
   def loan_has_balance
     loan_has_balance = (@loanamount > @payment)
     loan_has_balance
   end
 end

 loan = Loan.new(0, 0, 0.0)
 
 puts "Welcome to the Loan calculator."
 print "Please enter a loan amount: "
 @loanvalue = gets
 print "\n"
 
 print "What will be your payment amount: "
 @payment = gets
 print "\n"
 
 print "What is the interest rate: (0.065)"
 @rate = gets
 print "\n"
 
 loan = Loan.new(@loanvalue, @payment, @rate)
 
 loan.printheader
 counter = 0
 loan.printloan(counter)
while loan.loan_has_balance do
  loan.calculatepayment
  counter += 1
  loan.printloan(counter)
end
  #this will tie up any balance that may be left.
  if loan.loanamount > 0
    loan.last_payment 
    loan.printloan(counter + 1)
  end
